<?

require_once ("application.fieldset.inc");

global $_GLOBAL_DBH;
global $_OBJECT_IFACE_PANEL;

$assignment_id = $_REQUEST["assignment_id"];

if ($_REQUEST["pta_status"] == "assign") {

	$assignment_label = "Assign";
	$user_id = $_APP_SESSION_USER->id;
	$task_id = $_REQUEST["task_id"];
	$assigned_id = null;
} else {

	$task_assigment = $_GLOBAL_DBH->query ("SELECT user_id,task_id FROM project_task_assignment WHERE id={$_REQUEST["assignment_id"]}");
	$task_assigment = $_GLOBAL_DBH->fetch_object ($task_assigment);

	$task_id = $task_assigment->task_id;

	if ($_REQUEST["pta_status"] == "accept") {

		$assignment_label = "Accept";
		$user_id = $_APP_SESSION_USER->id;
		$assigned_id = $task_assigment->user_id;
	} elseif ($_REQUEST["pta_status"] == "accept") {

		$assignment_label = "Accept";
		$user_id = $_APP_SESSION_USER->id;
		$assigned_id = $task_assigment->user_id;
	} elseif ($_REQUEST["pta_status"] == "reject") {

		$assignment_label = "Reject";
		$user_id = $_APP_SESSION_USER->id;
		$assigned_id = $task_assigment->user_id;
	} elseif ($_REQUEST["pta_status"] == "forward") {

		$assignment_label = "Forward";
		$user_id = $_APP_SESSION_USER->id;
	}
}

$fieldset_assignment = new Fieldset (array (
	db_conn=>$_GLOBAL_DBH,
	relation=>"project_task_assignment",
	prefix=>"pta_",
	fieldset=>array (
		array (name=>"id",label=>"ID",type=>"numeric",
				value=>$_REQUEST["assignment_id"],
				input=>"hidden",primary_key=>true),
		array (name=>"task_id",label=>"Task ID",type=>"numeric",
				value=>$task_id,
				input=>"hidden"),
		array (name=>"project_id",label=>"Project ID",type=>"numeric",
				value=>$_REQUEST["project_id"],
				input=>"hidden"),
		array (name=>"user_id",label=>"User ID",type=>"numeric",
				value=>$user_id,
				input=>"hidden"),
		array (name=>"assignment_id",label=>"Assignment ID",
				optional=>true,
				type=>"numeric",input=>"hidden",
				value=>$_REQUEST["assignment_id"]),
		array (name=>"assigned_id",label=>"Select Assinged User",type=>"numeric",
				relation=>array(foreign=>"users",key=>"id",
						field=>"name"),
				value=>$assigned_id,
				input=>"select",
				options=>"_app_select_query",
				options_params=>"SELECT id,CONCAT(CONCAT(last,' ',first),' :: ',name) AS title FROM users",
				columns=>array (key=>"id",value=>"title"),
		),
		array (name=>"status",label=>"Assignment Status",type=>"character",
				input=>"select",
				value=>$_REQUEST["pta_status"],
				options=>array (
					array (id=>"accept",name=>"Accept"),
					array (id=>"assign",name=>"Assign"),
					array (id=>"reject",name=>"Reject"),
					array (id=>"forward",name=>"Forward"),
				),
				columns=>array(key=>"id",value=>"name"),
		),
		array (name=>"details",label=>"Details",type=>"alphanumeric",
				optional=>true,
				input=>"textarea",rows=>5),
	)

));

$fieldset_assignment->parse ();

$return = "/fs_projects.php?fs_tab_tasks=assignment&project_id={$_REQUEST["project_id"]}&fs_tab=tasks&fs_tab_option=fs_tasks&fs_action=view&task_id={$_REQUEST["task_id"]}";

$fs_options = array (
	addnew=>array (label=>"Add New"),
	create=>array (label=>$assignment_label),
	modify=>array (label=>"Modify"),
	remove=>array (label=>"Remove"),
	update=>array (label=>"Update"),
	cancel=>array (label=>"Cancel"),
	delete=>array (label=>"Delete",
			redirect=>dirname ($_SERVER["PHP_SELF"]) . $return),
	canret=>array (label=>"Cancel",
			redirect=>dirname ($_SERVER["PHP_SELF"]) . $return),
	close=>array (label=>"Close",
			redirect=>dirname ($_SERVER["PHP_SELF"]) . $return),
);

$image_options = array (size=>$_REQUEST["_img_size"]);

list ($fs_input, $fs_action, $store, $valid, $success) =

	_app_process_fieldset (array (
		fieldset=>$fieldset_assignment,
		fs_options=>$fs_options,
		fs_primary_key=>"assignment_id",
		fs_action=>($_REQUEST["fs_action_assignment"] ?
				$_REQUEST["fs_action_assignment"] :
				"view"),
		fs_action_field=>"fs_action_assignment",
		fs_prefix=>"pta_",
));

if (((isset ($assignment_id) && ($assignment_id > 0)) || $_REQUEST["pta_status"] == "assign") && $store && $success) {

	$_GLOBAL_DBH->query ("UPDATE project_task_assignment SET close='true' WHERE id={$assignment_id}");
	if ($_REQUEST["pta_status"] == "assign") {
	
		$_GLOBAL_DBH->query ("UPDATE project_tasks SET status='processing' WHERE id={$task_id}");
		$fieldset_tasks->get_field ("status")->set_variable ("value", "processing");
	}
	elseif ($_REQUEST["pta_status"] == "accept") $_GLOBAL_DBH->query ("UPDATE project_tasks SET current_id={$_APP_SESSION_USER->id},status='assigned' WHERE id={$task_id}");
}

$_OBJECT_FIELDSET_CONTROL =_app_create_fieldset_object (array (
	fieldset_id=>"fs_project_task_assignment",
	db_conn=>$_GLOBAL_DBH,
	fieldset=>$fieldset_assignment,
	fs_action=>$fs_action,
	fs_input=>$fs_input,
	fs_link=>"fs_projects.php?fs_tab=tasks&project_id={$_REQUEST["project_id"]}&fs_tab_option=fs_tasks&fs_action=view&fs_tab_tasks=assignment&fs_tab_option_tasks=fs_assignment&task_id={$_REQUEST["task_id"]}&assignment_id={$_REQUEST["assignment_id"]}&pta_status={$_REQUEST["pta_status"]}",
	fs_options=>$fs_options,
	fs_action_field=>"fs_action_assignment",
	fs_prefix=>"pta_",
));

$_OBJECT_IFACE_TABBING_PANEL->insert_container ($_OBJECT_FIELDSET_CONTROL);

?>
